package com.ruiysoft.controller.live;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by liudh on 2019/1/15.
 */

@RestController
public class LivecodemanageController {
    @RequestMapping(value = "/getLivePlayerUrl",method = RequestMethod.GET)
    public Object getLivePlayerUrl(String shopId) {
        return "rtmp://live.avicyt.com/live/" + shopId;
    }

    @RequestMapping(value = "/getLivePushUrl",method = RequestMethod.GET)
    public Object getLivePushUrl(String shopId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {

            Date date = sdf2.parse(sdf.format(new Date()) + " 23:59:59");
            long txTime = date.getTime() / 1000;
            return getPushUrl("38747.livepush.myqcloud.com",
                    "b22c4934f304fcf3800d9760359f45d5",
                    shopId,
                    txTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static final char[] DIGITS_LOWER =
            {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

    /*
     * KEY+ stream_id + txTime
     */
    String getPushUrl(String domain,String key, String streamId, long txTime) {
        String input = new StringBuilder().
                append(key).
                append(streamId).
                append(Long.toHexString(txTime).toUpperCase()).toString();

        String txSecret = null;
        try {
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            txSecret  = byteArrayToHexString(
                    messageDigest.digest(input.getBytes("UTF-8")));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        return txSecret == null ? "" :
                new StringBuilder().
                        append("rtmp://").
                        append(domain).
                        append("/live/").
                        append(streamId).
                        append("?txSecret=").
                        append(txSecret).
                        append("&").
                        append("txTime=").
                        append(Long.toHexString(txTime).toUpperCase()).
                        toString();
    }

    String byteArrayToHexString(byte[] data) {
        char[] out = new char[data.length << 1];

        for (int i = 0, j = 0; i < data.length; i++) {
            out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4];
            out[j++] = DIGITS_LOWER[0x0F & data[i]];
        }
        return new String(out);
    }
}
